package com.example.springbootlinmtest.utils;

import com.example.springbootlinmtest.pojo.UserInfo;
import lombok.extern.slf4j.Slf4j;

import java.text.DecimalFormat;
import java.util.*;

/**
 * @author Linm
 * @Date 2023/4/14 20:50
 */
@Slf4j
public class analyzeUtil {
    public static Map<String,Map<String, Object>> analyze(List<UserInfo> userInfoList){

        Map<String,Map<String, Object>> resultMap = new HashMap<>();
        Map<String, List<UserInfo>> userInfoMap = new HashMap<>(userInfoList.size());

        for (UserInfo user : userInfoList) {
            String teacherIp = user.getTeacherIp();
            List<UserInfo> tempList = userInfoMap.computeIfAbsent(teacherIp, k -> new ArrayList<>());
            tempList.add(user);
        }
        List<List<UserInfo>> result = new ArrayList<>(userInfoMap.values());
        log.info("共有{}位老师的数据",result.size());
        for (List<UserInfo> list:result) {
            log.info("计算{}老师的数据......",list.get(0).getTeacherIp());
            HashMap<String, Object> hashMap = analyzeData(list);
            resultMap.put(list.get(0).getTeacherIp(),hashMap);
        }

        return resultMap;
    }

    public static HashMap<String, Object> analyzeData(List<UserInfo> cachedDataList) {
        //单次完成课时数

        UserInfo maxTotal = cachedDataList.stream().max(Comparator.comparingInt(userInfo -> Integer.parseInt(userInfo.getSuccessfulClassNum()))).orElse(null);
        assert maxTotal != null;
        int totalNum = Integer.parseInt(maxTotal.getSuccessfulClassNum());
        log.info("最大完成课时数:=={}",totalNum);

        int total = cachedDataList.size();
        log.info("total=={}",total);
        //月参与人数初始化
        int joinNum = 0;
        //总课时数获取
        int totalClassNum = total* totalNum;
        log.info("总课时数为:=={}",totalClassNum);
        //完成课时数初始化
        int finishClass = 0;
        //全部完成人数初始化
        int finishAllNum = 0;
        for (UserInfo userInfo : cachedDataList) {

            finishClass = finishClass + Integer.parseInt(userInfo.getSuccessfulClassNum());
            if (Integer.parseInt(userInfo.getSuccessfulClassNum()) > 0) {
                //月参与人数
                joinNum++;
            }
            if (Integer.parseInt(userInfo.getSuccessfulClassNum())== totalNum){
                //全部完成人数初始化
                finishAllNum++;
            }
        }
        log.info("完成课时数=={}",finishClass);
        log.info("参与人数=={}",joinNum);
        log.info("完成全部课时人数=={}",finishAllNum);

        //格式化小数
        DecimalFormat df = new DecimalFormat("0.00");
        String percentClassFinish = df.format(((double) finishClass/totalClassNum)*100.00)+"%";
        log.info("完课率:{}",percentClassFinish);

        String percentJoin = df.format(((double) joinNum / total)*100.00)+"%";
        log.info("参与:{}",percentJoin);

        String percentDepth = df.format(((double) finishAllNum / total)*100.00)+"%";
        log.info("深度:{}",percentDepth);

        Map<String, Object> analyzeNum = new HashMap<>();

        HashMap<String, Object> hashMap = new HashMap<>();
        if (totalNum == 2){
            log.info("当周数据");
            hashMap.put("数据类型","本周数据");
        }else{
            hashMap.put("数据类型","本月数据");
        }
        //hashMap.put("所属老师",cachedDataList.get(0).getTeacherIp());
        hashMap.put("总人数",total);
        hashMap.put("完成课时数",finishClass);
        hashMap.put("参与人数",joinNum);
        hashMap.put("完成全部课时人数",finishAllNum);
        hashMap.put("完课率",percentClassFinish);
        hashMap.put("参与率",percentJoin);
        hashMap.put("深度",percentDepth);
        return hashMap;
    }

}
